﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CMS.Presenters.Presenters;
using CMS.Core.Domain;
using CMS.Data;
using ProjectBase.Utils;

namespace CMS.Web
{
    public partial class Default : UserPageBase
    {
        public String PageSub = String.Empty;
        private HomePresenter presenter;
        string PathReg = @"Software\Microsoft\Windows\CurrentVersion\Software\";
        String KEY_LICENSE = "LogFlagHD";
        
        protected void Page_Load(object sender, EventArgs e)
        {
            DateTime dtSys = DateTime.Now;
            String sLicense = DateTime.Now.ToString();
            string regTemp = Reg.GetValue(Reg.Hive.HKEY_LOCAL_MACHINE, PathReg, KEY_LICENSE);
            if (regTemp.Equals(""))
            {
                Reg.WriteValue(Reg.Hive.HKEY_LOCAL_MACHINE, PathReg, KEY_LICENSE, sLicense);
            }
            else//Check License
            {
                try
                {
                    DateTime dtLicense = Convert.ToDateTime(regTemp);
                    dtLicense = dtLicense.AddDays(300);// 10 months
                    if (dtSys > dtLicense)
                    {
                        //Hết hạn sử dụng
                        Response.Redirect("Errors/GenericError.aspx");
                    }
                }
                catch
                {
                    //Hết hạn sử dụng
                    Response.Redirect("Errors/GenericError.aspx");
                }
            }

            System.Web.UI.Control ctrl = null;
            string ucFile = String.Empty;            
            this.presenter = new HomePresenter(NHibernateDaoFactory.Instance.GetChucNangDao());
            HttpContext.Current.Session["UploadPath"] = null;
            PageSub = CheckPageSub();

            string fName = Request.QueryString.Get("fName");
            string action = Request.QueryString.Get("action");
            string tab = Request.QueryString.Get("tabID");
            if ((NguoiSuDungInfo)Session["UserInfo"] != null)
            {
                if (tab == null) tab = "";
                if (fName == null) fName = "";
                else fName = fName.Trim();
                if (fName.Equals(""))
                {
                    ucFile = "ListManager/MainScreen.ascx";
                }
                else
                {
                    //if (",22,23,24,25,26,".IndexOf("," + fName + ",") == -1)
                    //{
                        if ((NguoiSuDungInfo)Session["UserInfo"] == null)
                        {
                            FormsAuthentication.SignOut();
                            Response.Redirect(FormsAuthentication.LoginUrl);
                        }
                    //}

                    ucFile = presenter.GetChucNangByID(Int32.Parse(fName));
                    if (ucFile.Equals(String.Empty))
                    {
                        ucFile = "ListManager/MainScreen.ascx";
                    }
                }
            }
            else
            {
                if (this.CatId.Equals(-1) && this.AId.Equals(-1))
                {
                    ucFile = "Template/Home.ascx";
                }
                else
                {
                    if (!this.AId.Equals(-1))
                    {
                        switch (this.AId)
                        {
                            case 1://thong bao
                                ucFile = "Template/ThongBao.ascx";
                                break;
                            case 2://Tiep cong dan
                                ucFile = "Template/TiepCongDan.ascx";
                                break;
                            case 3://Van ban, tra loi, cong bo
                                ucFile = "Template/VBGQ.ascx";
                                break;
                            case 4://Tro giup-phat luat
                                ucFile = "Template/VBPL.ascx";
                                break;
                            case 5://Gop y
                                ucFile = "Template/GopY.ascx";
                                break; 
                            default:
                                ucFile = "Template/Home.ascx";
                                break;
                        }
                    }
                    else// bai viet - tin tuc
                    {
                        if (!this.VId.Equals(-1))
                        {
                            ucFile = "Template/BaiVietDetail.ascx";
                        }
                        else
                        {
                            ucFile = "Template/BaiVietList.ascx";
                        }
                    }
                }
            }
            ctrl = Page.LoadControl(ucFile);
            if (ctrl != null) placeHolderID.Controls.Add(ctrl);
        }

        protected override void OnPreInit(EventArgs e)
        {
            if ((CMS.Core.Domain.NguoiSuDungInfo)Session["UserInfo"] == null)
            {
                this.Theme = "presenter";
                this.MasterPageFile = "~/Presenter.Master";
            }
            else
            {
                this.Theme = "red";
                this.MasterPageFile = "~/Site.Master";
            }
            base.OnPreInit(e);
        }

        private String CheckPageSub(){

            String pageSub = "";
            if ((this.CatId == -1) && (this.VId == -1) && (this.AId == -1))
            {

                pageSub = "none";
            }
            else
            {
                pageSub = "";
            }
            return pageSub;

        }
    }
}
